/*
 * *****************************************************************************************
 *
 * 		Copyright (C) 2014-2019 Gowin Semiconductor Technology Co.,Ltd.
 *
 * @file		task_app.c
 * @author		Embedded Development Team
 * @version		V1.0.0
 * @date		2019-10-1 09:00:00
 * @brief
 ******************************************************************************************
 */

#include "gw1ns4c.h"

#include "uart_app.h"
#include "printf.h"
#include "led.h"
#include "task_app.h"

#include "FreeRTOS.h"
#include "task.h"


#define SW_STR_NAME							"freertos_1ns2_keil"
#define SW_STR_EDITION						"V1.0.0.190611a"
#define SW_STR_AUTHOR						"GW"


#define LOGO_PRINT_ON

#ifdef LOGO_PRINT_ON
#include "logo.h"
#endif	//LOGO_PRINT_ON


#define TASK_DELAY_MS_TO_TICK(ms)		((ms) / (1000 / configTICK_RATE_HZ))


#define STARTUP_TASK_PRIO			(tskIDLE_PRIORITY + 2)
#define STARTUP_TASK_STK_SIZE 		64
TaskHandle_t startup_task_Handler;
static void startup_task(void *pvParameters);

#define APP_TASK_PRIO				(tskIDLE_PRIORITY + 1)
#define APP_TASK_STK_SIZE 			64
TaskHandle_t app_task_Handler;
static void app_task(void *pvParameters);


extern void xPortSysTickHandler(void);


void SysTick_Handler(void)
{
	if (taskSCHEDULER_NOT_STARTED != xTaskGetSchedulerState())
    {
        xPortSysTickHandler();	
    }
}

static void printf_str(const char *str)
{
#ifdef PRINTF_USE
	printf_d(str);
#else
	UART_SendString(UART_CONSOLE, (char *)str);
#endif	//PRINTF_USE
	//UART_SendString(UART1, (char *)str);
	//UART_SendString(UART2, (char *)str);
	//UART_SendString(UART3, (char *)str);
}

static void sw_edition_print(void)
{
	printf_str("\r\n");
	printf_str(starts_str);
	printf_str("Name:     "SW_STR_NAME"\r\n"
           "Edition:  "SW_STR_EDITION"\r\n"
		   "Compiled: "__DATE__", "__TIME__"\r\n"
			"Author:   "SW_STR_AUTHOR"\r\n");
	printf_str(starts_str);
	printf_str("\r\n");
}

static void start_info_print(void)
{
	sw_edition_print();

#ifdef LOGO_PRINT_ON
	printf_str("\r\n");
	printf_str(starts_str);
	printf_str(logo_str);
	printf_str(starts_str);
	printf_str("\r\n");
#endif	//LOGO_PRINT_ON
}

static void init_misc(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);

	uart_app_init(UART_CONSOLE_BAUDRATE);
	led_init();

	start_info_print();
}

static void app_task(void *pvParameters)
{
    while (1)
	{
		led_toggle(1);

		printf_str("1.app_task\r\n\r\n");
		vTaskDelay(TASK_DELAY_MS_TO_TICK(1000));
	}
}

static void startup_task(void *pvParameters)
{
	taskENTER_CRITICAL();
	{
		xTaskCreate((TaskFunction_t )app_task,
					(const char *   )"app_task",
					(uint16_t       )APP_TASK_STK_SIZE,
					(void *         )NULL,
					(UBaseType_t    )APP_TASK_PRIO,
					(TaskHandle_t * )&app_task_Handler);
	}		
    taskEXIT_CRITICAL();
	
    while (1)
	{
		led_toggle(0);

		printf_str("0.startup_task\r\n");
		vTaskDelay(TASK_DELAY_MS_TO_TICK(250));
	}
}

void task_app(void)
{
	init_misc();
	
	taskENTER_CRITICAL();
	{
		xTaskCreate((TaskFunction_t )startup_task,
					(const char *   )"startup_task",
					(uint16_t       )STARTUP_TASK_STK_SIZE,
					(void *         )NULL,
					(UBaseType_t    )STARTUP_TASK_PRIO,
					(TaskHandle_t *  )&startup_task_Handler);
	}		
    taskEXIT_CRITICAL();

    vTaskStartScheduler();

	while (1)
	{
		;
	}
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
